<template>
  <div class="collect-iframe-container" :loading="loading">
    <iframe :src="iframeUrl" width="100%" height="100%" @load="reset" frameborder="0"></iframe>
    <!-- <iframe :src="http://192.168.2.106:8080" width="100%" height="100%" @load="reset" frameborder="0"></iframe> -->
  </div>
</template>

<script lang="ts" setup>
import { ref, computed, watch, reactive, onMounted, nextTick } from "vue";
import request from "@/api/index";
import getters from "@/store/getters";
import { useRouter, useRoute } from "vue-router";
import { routerPerm } from '@/utils/common';

// data
const _path = import.meta.env.VITE_APP_API_HOST;
const route = useRoute();
const loading = ref<boolean>(true);
const identity = localStorage.getItem('yunIdentity');// 云端用户ID
const iframeUrl = ref<any>('');// 云端url
const appid = localStorage.getItem('appid');// 客户端APPID
const theme = ref<any>('');// 主题
const userInfo = getters.userInfo();
const siteId = ref(userInfo.siteId);// 子站ID
const cmsData = ref<any>(null);
// methods
const reset = () => {
  loading.value = false
};
// 获取模型和APPID
const getAppIDOrModel = () => {
  request.fetchCollectGetId().then((res: any) => {
    console.log(res, '获取模型和APPID');
    cmsData.value = res
    window.localStorage.setItem('appid', res.appId)
    setYunAppIdOrModel()
  })
};
// 设置云端模型和栏目
const setYunAppIdOrModel = () => {
  let obj = Object.assign({ 'identity': identity, 'siteId': siteId.value }, cmsData.value)
  request.fetchSetYunAppIdOrModel(obj).then((res: any) => {})
};

const init = () => {
  let urlText = ''
  getAppIDOrModel()
  if(route.query.collectType !== undefined) {
    urlText += '&taskId=' + route.query.taskId
    if(route.query.collectType === 'Start' || route.query.collectType === 'See') {
      urlText += '&to=FastCollectLog'
    }else if (route.query.collectType === 'Edit') {
      urlText += '&to=' + route.query.to
    }
  }
  let host = _path || window.location.protocol + '//' + window.location.host
  let protocol = host.indexOf('https') > -1 ? 'https' : 'http'
  iframeUrl.value = protocol + '://cj2.yun.jeecms.com' + '?appId=' + appid + '&siteId=' + siteId.value + '&identity=' + identity + urlText
};
//cj.yun.jeecms.com/
// cj.x2.jeecms.com/
const getCmsData = (taskId: any) => {
  
};

// onMounted
onMounted(() => {
  window.addEventListener('message', function(event: any) {
    if(event.data.taskId) {
      // 此处执行事件
    getCmsData(event.data.taskId)
    }
  })
  init()
})
</script>

<style lang="scss">
.collect-iframe-container{
  height: 100%;
  overflow-y: auto;
  padding-bottom: 40px;
}

</style>
